<template>
  <div
    v-if="!loading"
    class="vc-app-list-goods-double"
    :style="visual.parseStyle(compStyle)"
  >
    <div
      v-if="dataList && dataList.length > 0"
      class="vc-app-list-goods-double-list"
    >
      <div
        v-for="(data, index) in dataList"
        :key="index"
        class="vc-app-list-goods-double-item"
      >
        <div style="text-align: center">
          <img v-if='data["图片"]' :src='imageUrl + data["图片"]' alt />
          <cc-icon v-else iconId="018ece4f76dbdc838868" size="120" />
        </div>
        <div class="item-content">
          <div class="item-line">
            <div class="item-name-label" v-if='data["名称标签"]'>{{data["名称标签"]}}</div>
            <div class="item-name ellipsis">{{data["名称"]}}</div>
          </div>
          <div class="item-line">
            <div class="item-label ellipsis">{{(data["标签"] || '').replace(/,/g, ' ')}}</div>
          </div>
          <div class="item-line item-baseline">
            <div class="item-red item-smaller">￥</div>
            <div class="item-red item-bigger">{{tool.getMoneyInt(data["价格"])}}</div>
            <div class="item-red item-smaller item-mr5">.{{tool.getMoneyScale(data["价格"])}}</div>
            <div class="item-red item-smaller item-mr5" v-if='data["价格标签"]'>{{data["价格标签"]}}</div>
            <div class="item-grey item-smaller ellipsis">{{data["价格补充"]}}</div>
          </div>
        </div>
      </div>
    </div>
    <div
      v-else
      class="vc-app-list-goods-double-empty"
      :style="visual.parseStyle(compStyle)"
    >
      <span style="margin-top: 20px;">暂无数据</span>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, nextTick, onMounted } from 'vue';
import visual from "@/visual/js/visual";
import tool from "@/utils/tool";

const props = defineProps({
  node: Object,
  compStyle: Object,
  compData: Object
});
const currNode = ref(props.node);
const dataList = ref([]);
const loading = ref(true);
const imageUrl = ref(import.meta.env.VITE_IMAGE_PATH);

const init = async() => {
  loading.value = true;
  if (currNode.value.compData.dataSourceType === '') {
    currNode.value.compData.dataSourceType = '静态数据';
  }
  await nextTick(() => loading.value = false);
};

const loadData = async() => {
  dataList.value = (await visual.loadData(props.compData, {}, {}, currNode.value)).data || [];
  currNode.value.compData.dataList = JSON.stringify(dataList.value);
};

watch(() => props.node.compData?.refresh, () => {
  loadData();
});

onMounted(() => {
  init();
  loadData();
});
</script>

<style scoped lang="scss">
.vc-app-list-goods-double {
  width: 100%;
  display: flex;
  flex-direction: row;
  background: #F5F6FA;
  .vc-app-list-goods-double-list {
    width: 1px;
    flex-grow: 1;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    overflow: auto;
    padding: 10px;
    gap: 10px;
    .vc-app-list-goods-double-item {
      white-space: nowrap;
      width: calc(50% - 5px);
      display: flex;
      flex-direction: column;
      border-radius: 5px;
      background: white;
      padding-bottom: 3px;
      img {
        max-width: 100%;
        max-height: 100%;
        border-radius: 5px 5px 0 0;
      }
      .item-content {
        padding: 5px;
        .ellipsis {
          text-overflow: ellipsis;
          white-space: nowrap;
          overflow: hidden;
        }
        .item-red {
          color: #FF3333;
        }
        .item-grey {
          color: #8c939d;
        }
        .item-smaller {
          font-size: 12px;
        }
        .item-bigger {
          font-size: 20px;
          font-weight: bold;
        }
        .item-baseline {
          align-items: baseline!important;
        }
        .item-line {
          display: flex;
          flex-direction: row;
          align-items: center;
        }
        .item-name-label {
          background: #FF3333;
          color: white;
          font-size: 10px;
          border-radius: 5px;
          padding: 2px 5px;
          margin-right: 5px;
        }
        .item-name {
          flex-grow: 1;
          width: 1px;
        }
        .item-label {
          color: #D97E23;
          font-size: 12px;
          height: 25px;
          line-height: 25px;
        }
        .item-mr5 {
          margin-right: 5px;
        }
      }
    }
  }
  .vc-app-list-goods-double-list::-webkit-scrollbar {
    display: none;
  }
  .vc-app-list-goods-double-icon {
    width: 30px;
    margin-left: 10px;
    border-left: 1px solid #ccc;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .vc-app-list-goods-double-empty {
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>